unit con_NotaVenda;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Formulario_Padrao_Consulta, StdCtrls, Buttons, ExtCtrls, Grids, DBGrids, dbtables, db,
  Mask, ToolEdit, TFlatButtonUnit, DBClient, Provider;

type
  TFrmConNotaVenda = class(TFrmConsulta)
    DbgNotaVenda: TDBGrid;
    Label1: TLabel;
    EdtData_inicial: TDateEdit;
    dsp: TDataSetProvider;
    CDS: TClientDataSet;
    procedure EdtNumNotaKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure BtnFecharClick(Sender: TObject);
    procedure EdtData_inicialKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);

  private
    { Private declarations }

  public
    { Public declarations }
    nota: string;
    serie: string;
    entidade: string;
    codEmpresa: string;
    procedure Fazconsulta;

  end;

var
  FrmConNotaVenda: TFrmConNotaVenda;

implementation

uses
  clsNotaVenda, dm, clsBackupVenda, clsParametros;

var
  NotaVenda: TNotaVenda;
  BackupVenda: TBackupVenda;
  Tq: TQuery;
  Tds: TDataSource;

{$R *.DFM}

procedure TFrmConNotaVenda.EdtNumNotaKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
  inherited;
  if key = 12 then
    FazConsulta;
end;

procedure TFrmConNotaVenda.Fazconsulta;
var
  Parametros: TParametros;
begin
  Parametros := TParametros.Create;
  Parametros.Recuperar('1');
  tq.close;
  CDS.Close;
  if entidade = '' then
    backupvenda.consultarNota(tq, EdtData_inicial.Date)
  else
    backupvenda.consultarNotaEntidade(entidade, EdtData_inicial.Date, tq);
  if (serie = Parametros.Serie_Temporaria) and (serie <> '') then
    backupvenda.consultarNotaSerie(serie, EdtData_inicial.Date, tq);
  Parametros.Free;
  tq.open;
  CDS.Open;
  {if codEmpresa <> '' then begin
    CDS.Filtered := False;
    CDS.Filter := 'emp_codigo = ' + QuotedStr(codEmpresa);
    CDS.Filtered := True;
  end;}

  DbgNotaVenda.DataSource := tds;
  DbgNotaVenda.Refresh;
end;

procedure TFrmConNotaVenda.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  inherited;
  NotaVenda.free;
  BackupVenda.free;
end;

procedure TFrmConNotaVenda.FormCreate(Sender: TObject);
begin
  inherited;
  NotaVenda := TNotaVenda.create;
  BackupVenda := TBackupVenda.create;
end;

procedure TFrmConNotaVenda.FormActivate(Sender: TObject);
begin
  inherited;
  tq := TQuery.create(DmInt.Db);
  tq.DatabaseName := DmInt.Db.DatabaseName;
  tq.Active := false;
  tds := TDataSource.create(self);
  dsp.DataSet := tq;
  CDS.ProviderName := 'dsp';
  Tds.DataSet := CDS;
  fazconsulta;
end;

procedure TFrmConNotaVenda.BtnFecharClick(Sender: TObject);
begin
  inherited;
  serie := CDS.fieldbyname('bkp_serie').AsString;
  nota := CDS.fieldbyname('bkp_numero').AsString;
  entidade := CDS.fieldbyname('ent_codigo').AsString;
end;

procedure TFrmConNotaVenda.EdtData_inicialKeyDown(Sender: TObject;
  var Key: Word; Shift: TShiftState);
begin
  inherited;
  if key = 12 then begin
    FazConsulta;
    EdtData_inicial.setfocus;
  end;
end;

end.
